<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\facade\Builder;
use app\common\model\Area;
use app\common\model\ProductCate;

use think\App;
use think\facade\View;
use think\response\Json;

class HotCities extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\HotCities';

    //验证器
    protected string $validate = 'HotCities';

    //关联模型
    protected array $hasOne = ['p', 'c'];

    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'cate_1', 'title' => '省', 'templet' => '<div>{{d.p.title}}</div>'])
            ->addColumn(['field' => 'cate_1', 'title' => '市', 'templet' => '<div>{{d.c.title}}</div>'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['size' => '600,1200'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '600,1200'], 'delete'])
            ->addSwitchTool([
            ])
            ->fetch();
    }


    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['province_name'] = Area::where('code',$param['province'])->value('province');
        $param['city_name'] = Area::where('code',$param['city'])->value('city');
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public function add($tpl = 'hot_cities/edit'): string
    {
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'hot_cities/edit', $info = ''): string
    {
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

}
